package com.example.webdemo.dao.Impl;

import com.example.webdemo.bean.user;
import com.example.webdemo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public UserDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public user findByUsername(String username) {
        try {
            String sql = "SELECT * FROM user WHERE username = ?";
            return jdbcTemplate.queryForObject(sql, new UserRowMapper(), username);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public user findById(int id) {
        String sql = "SELECT * FROM user WHERE user_id = ?";
            return jdbcTemplate.queryForObject(sql, new UserRowMapper(), id);
    }

    @Override
    public int save(user user) {
        String sql = "INSERT INTO user(username, password, real_name, email, phone) " +
                "VALUES(?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getReal_name(),
                user.getEmail(),
                user.getPhone());
    }

    @Override
    public int update(user user) {
        String sql = "UPDATE user SET username = ?, real_name = ?, email = ?, phone = ?" +
                "WHERE user_id = ?";
        return jdbcTemplate.update(sql,
                user.getUsername(),
                user.getReal_name(),
                user.getEmail(),
                user.getPhone(),
                user.getUser_id());
    }

    @Override
    public int changepass(int id, String newpass) {
        String sql = "UPDATE user SET password = ?" +
                "WHERE user_id = ?";
        return jdbcTemplate.update(sql,
                newpass,
                id);
    }

    @Override
    public int delete(int id) {
        String sql = "DELETE FROM user WHERE user_id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public user findByEmail(String email) {
        String sql = "SELECT * FROM user WHERE email = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new UserRowMapper(), email);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public user findByResetToken(String token) {
        // 当前数据库结构中无reset_token字段，如需使用需先添加该字段
        return null;
    }

    @Override
    public List<user> findall() {
        String sql = "SELECT * FROM user ORDER BY user_id ";
        return jdbcTemplate.query(sql, new UserRowMapper());
    }
    // 自定义RowMapper
    private static final class UserRowMapper implements RowMapper<user> {
        @Override
        public user mapRow(ResultSet rs, int rowNum) throws SQLException {
            user user = new user();
            user.setUser_id(rs.getInt("user_id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setReal_name(rs.getString("real_name"));
            user.setEmail(rs.getString("email"));
            user.setPhone(rs.getString("phone"));
            user.setCreate_time(rs.getDate("create_time"));
            user.setUpdate_time(rs.getDate("update_time"));
            return user;
        }
    }
}